package com.itcpeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcpeng.dao.PermissionDao;
import com.itcpeng.dao.RoleDao;
import com.itcpeng.dao.UserDao;
import com.itcpeng.entity.PageResult;
import com.itcpeng.entity.QueryPageBean;
import com.itcpeng.pojo.Permission;
import com.itcpeng.pojo.Role;
import com.itcpeng.pojo.User;
import com.itcpeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {


    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private UserDao userDao;

    //用户的登陆认证和授权
    @Override
    public User findByUserName(String s) {
        return userDao.findUsername(s);
    }

      //根据用户名查询User
    @Override
    public User findUserByUsername(String username) {
        return userDao.findUserByUsername(username);
    }


  //根据用户id查询封装好了roles
    @Override
    public Set<Role> findRoleByUserId(Integer id) {
        Set<Role> roles = roleDao.findByUserId(id);
        for (Role role : roles) {
            //根据角色id查询相应的权限
            Set<Permission> permissions = permissionDao.findByRoleId(role.getId());
            role.setPermissions(permissions);
        }
        return roles;
    }


    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<User> page = userDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }



    //添加用户
    @Override
    public void add(User user, Integer[] roleIds) {
        //先添加用户
        System.out.println(user.getPassword());
        userDao.add(user);
        //添加中间表
        addUserAndRole(user.getId(), roleIds);
    }


    //添加检查组和检查项的联系
    public void addUserAndRole(Integer id, Integer[] roleIds) {
        if (roleIds != null && roleIds.length > 0) {
            for (Integer c : roleIds) {
                Map map = new HashMap();
                map.put("user_id", id);
                map.put("role_id", c);
                //调用业务添加联系
                userDao.addUserAndRole(map);
            }
        }
    }


    //查询检查组
    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return userDao.findRoleIdsByUserId(id);
    }


    //编辑检查组
    @Override
    public void edit(User user, Integer[] roleIds) {
        //先修改检查组
        userDao.edit(user);
        //先删除之前的联系
        userDao.deleteRoleAndUserByUserId(user.getId());
        //添加新的联系
        addUserAndRole(user.getId(), roleIds);
    }


    //删除检查组
    @Override
    public void delete(Integer id) {
        //先删除检查组与检查项之间的关联
        userDao.deleteRoleAndUserByUserId(id);
        //再删除检查组
        userDao.deleteById(id);

    }
}
